// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: Implementation of CXFormsElementButton.
//   $Id$

#include "stdafx.h"

#include "ButtonUI.h"
#include "FP_ReturnCodes.h"
#include "ImplButton.h"
#include "parseButton.h"
//////////////////////////////////////////////////////////////////////
// CXFormsElementButton
//////////////////////////////////////////////////////////////////////

CXFormsElementButton::CXFormsElementButton()
{
   m_sControlName = "trigger";
   m_bNewMethod = true;
   m_bIsButton = true;
}

CXFormsElementButton::~CXFormsElementButton()
{
//	detachEvents();
}

CImpl* CXFormsElementButton::CreateImpl()
{
	CImpl* pFormControl = (CImpl*) new CImplButton(this);

	return pFormControl;
}

CXFormsParse* CXFormsElementButton::CreateParse(CImpl* pImpl)
{
	CXFormsParse* pParse = new CXFormsParseButton((CImplButton*) pImpl);

	return pParse;
}


bool InSameDocument(IHTMLElement * pEl0,IHTMLElement * pEl1)
{

return true;
}

#include "MSHTML-Events.h"
const CComBSTR EVENTNAME_click(L"click");
const CComBSTR EVENTNAME_mousedown(L"mousedown");
const CComBSTR EVENTNAME_mouseup(L"mouseup");
const CComBSTR EVENTNAME_mouseout(L"mouseout");
const CComBSTR EVENTNAME_mouseover(L"mouseover");
const CComBSTR CSS_Class_Pressed(" pressed");
const CComBSTR CSS_Class_Unpressed(" unpressed");



HRESULT CXFormsElementButton::handleEvent(CComBSTR& sEventName, IHTMLEventObj2 * pEventObject,VARIANT * pvarResult)
{
	HRESULT hr = S_FALSE;
	hr = __super::handleEvent(sEventName, pEventObject, pvarResult);
	return hr;
}

void CXFormsElementButton::populateEvents(void)
{
//now handled by XFEL.
	
	m_lstEventNames.push_back(&EVENTNAME_click);
	m_lstEventNames.push_back(&EVENTNAME_mousedown);
	m_lstEventNames.push_back(&EVENTNAME_mouseup);
	m_lstEventNames.push_back(&EVENTNAME_mouseout);
	m_lstEventNames.push_back(&EVENTNAME_mouseover);
	
	__super::populateEvents();
}


	HRESULT CXFormsElementButton::attachEvents()
	{
		if(m_lstEventNames.size() == 0)
			populateEvents();

		HRESULT hr = __super::attachEvents();
		
		CComPtr<IUnknown> pUnk = GetControllingUnknown();
		CComQIPtr<IEventTarget> pThisAsTarget(pUnk);
		CComQIPtr<IEventListener> pThisAsListener(pUnk);
		if(pThisAsTarget)
		{
			CComBSTRPtrList::iterator i = m_lstEventNames.begin();
			
			while(i != m_lstEventNames.end() && SUCCEEDED(hr))
			{
				hr = pThisAsTarget->addEventListener((*i)->m_str,pThisAsListener,false);
				++i;
			}
		}
		return hr;
	};

	HRESULT CXFormsElementButton::detachEvents()
	{
		HRESULT hr = __super::detachEvents();
		CComQIPtr<IEventTarget> pThisAsTarget(GetControllingUnknown());
		CComQIPtr<IEventListener> pThisAsListener(GetControllingUnknown());
		if(pThisAsTarget)
		{
			CComBSTRPtrList::iterator i = m_lstEventNames.begin();
			
			while(i != m_lstEventNames.end() && SUCCEEDED(hr))
			{
				hr = pThisAsTarget->removeEventListener((*i)->m_str ,pThisAsListener,false);
				++i;
			}
		}
		
		return hr;
	};

	STDMETHODIMP CXFormsElementButton::handleEvent(IDOMEvent * pEvt)
	{
		HRESULT hr = S_FALSE;
		if(pEvt)
		{
			CComBSTR sEventName;
			pEvt->get_type(&sEventName);
			// Remove will this is not a formcontrol
			if(m_pFormControl)
			{
				if(sEventName == EVENTNAME_mouseout)
				{
					StyleUnHoverishly();
				}
                else if(sEventName == EVENTNAME_click)
				{
					CComQIPtr<IMouseEvent> pMouseEvent(pEvt);
					if(pMouseEvent)
					{
//						unsigned short buttonId;
//						pMouseEvent->get_button(&buttonId);
//						if(buttonId==1)
							hr = m_pFormControl->onactivate();
					}
				}
				else if(sEventName == EVENTNAME_mouseover)
				{
					StyleHoverishly();
				}
			}
		}
		return hr;
	}

	HRESULT CXFormsElementButton::ElementReady()
	{
		HRESULT hr = __super::ElementReady();
		if(SUCCEEDED(hr))
		{
			if(m_pImpl)
			{
				
				//Copy the appearance attribute into the className.
				CComPtr<IHTMLElement> pMaster;
				GetMasterElement(&pMaster);
				if(pMaster)
				{
					CComBSTR sClassName;
					pMaster->get_className(&sClassName);
					sClassName += " ";
					sClassName += m_pImpl->m_sAppearance;
					hr = pMaster->put_className(sClassName);
				}
				else
				{
					hr = E_FAIL;
				}
			}
		}
		return hr;
	}